import Vue from "vue";
import VueRouter from "vue-router";
import store from "../store/index";

import Index from "../views/Index.vue";
import Home from "../views/Home.vue";
import Sort from "../views/Sort.vue";
import SortDetail from "../views/SortDetail.vue";
import CustomerService from "../views/CustomerService.vue";
import MyCart from "../views/MyCart.vue";
import Favorites from "../views/Favorites.vue";
import Menu from "../views/Menu.vue";
import MenuDetail from "../views/MenuDetail.vue";
import Journalism from "../views/Journalism.vue";
import NewsDetail from "../views/NewsDetail.vue";
import Me from "../views/Me.vue";
import Wallet from "../views/Wallet.vue";
import Bill from "../views/Bill.vue";
import BillDetails from "../views/BillDetails.vue";
import Login from "../views/Login.vue";
import Register from "../views/Register.vue";
import Err from "../views/404.vue";

Vue.use(VueRouter);

const routes = [
  //加载广告
  { path: "/", name: "Index", component: Index, meta: { title: "夏日の粥品 - 美好的一天从早上一碗粥开始！" } },
  //404页面
  { path: "*", name: "Err", component: Err, meta: { title: "404 - 页面丢失" } },
  //首页
  { path: "/home", name: "Home", component: Home, meta: { title: "夏日の粥品 - 美好的一天从早上一碗粥开始！", keepAlive: true } },
  //分类
  { path: "/sort", name: "Sort", component: Sort, meta: { title: "分类 - 美好的一天从早上一碗粥开始！", keepAlive: true } },
  //分类详情
  { path: "/sort/sortDetail/:id", name: "SortDetail", component: SortDetail, meta: { title: "商品详情 - 美好的一天从早上一碗粥开始！", isBarred: true } },
  //购物车
  { path: "/myCart", name: "MyCart", component: MyCart, meta: { title: "购物车 - 美好的一天从早上一碗粥开始！", isBarred: true } },
  //联系客服
  { path: "/customerService", name: "CustomerService", component: CustomerService, meta: { title: "我的客服 - 美好的一天从早上一碗粥开始！", keepAlive: true } },
  //收藏夹
  { path: "/favorites", name: "Favorites", component: Favorites, meta: { title: "收藏夹 - 美好的一天从早上一碗粥开始！" } },
  //菜谱
  { path: "/menu", name: "Menu", component: Menu, meta: { title: "菜谱 - 美好的一天从早上一碗粥开始！", isBarred: true , keepAlive: true } },
  //菜谱详情
  { path: "/menu/menuDetail/:id", name: "MenuDetail", component: MenuDetail, meta: { title: "菜谱详情 - 美好的一天从早上一碗粥开始！" } },
  //新闻     可可
  { path: "/journalism", name: "Journalism", component: Journalism, meta: { title: "新闻 - 美好的一天从早上一碗粥开始！", keepAlive: true } },
  //新闻详情   可可
  { path: "/journalism/newsDetail/:index", name: "NewsDetail", component: NewsDetail, meta: { title: "新闻详情 - 美好的一天从早上一碗粥开始！" } },
  //我的
  { path: "/me", name: "Me", component: Me, meta: { title: "个人中心 - 美好的一天从早上一碗粥开始！" } },
  //钱包
  { path: "/wallet", name: "Wallet", component: Wallet, meta: { title: "我的钱包 - 美好的一天从早上一碗粥开始！", isBarred: true } },
  //账单
  { path: "/bill", name: "Bill", component: Bill, meta: { title: "我的账单 - 美好的一天从早上一碗粥开始！", isBarred: true } },
  //订单信息
  { path: "/billDetails/:index", name: "BillDetails", component: BillDetails, meta: { title: "订单信息 - 美好的一天从早上一碗粥开始！", isBarred: true } },
  // 登陆
  { path: "/login", name: "Login", component: Login, meta: { title: "用户登陆 - 美好的一天从早上一碗粥开始！" } },
  // 注册
  { path: "/register", name: "Register", component: Register, meta: { title: "用户注册 - 美好的一天从早上一碗粥开始！" } },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});

// 解决重定向的报错
const originalPush = VueRouter.prototype.push;
 
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err);
}

// 全局前置守卫   主要 用于登录验证
/**
 * @param {to} 将要去的路由
 * @param {from} 出发的路由
 * @param {next} 执行下一步
 */
router.beforeEach((to, _, next) => {
  // document.title = to.meta.title || '卖座电影';
  //判断当前路由是否需要进行权限控制
  if (to.meta.isBarred && !store.state.islogin) {
    next({ path: "/login" });
  } else {
    document.title = to.meta.title;//修改页签
    next();//允许进入
  }
});

export default router;
